﻿#region Usings

using System;
using System.Collections.Generic;

#endregion

namespace Rail.Net.Common.Extensions
{
    public static class EnumerableExtensions
    {
        public static IEnumerable<Tuple<T, T>> ConsecutivePairs<T>(this IEnumerable<T> sequence) where T : class
        {
            // Omitted nullity checking; would need an extra method to cope with
            // iterator block deferred execution
            using (var iterator = sequence.GetEnumerator())
            {
                if (!iterator.MoveNext())
                {
                    yield break;
                }
                var previous = iterator.Current;
                while (iterator.MoveNext())
                {
                    if (iterator.Current != null)
                    {
                        yield return Tuple.Create(previous, iterator.Current);
                        previous = iterator.Current;
                    }
                }
            }
        }
    }
}